﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:out_storage_detail
	/// </summary>
	public partial class out_storage_detail
	{
		public out_storage_detail()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("out_storage_detail_Id", "out_storage_detail"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int out_storage_detail_Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from out_storage_detail");
			strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = out_storage_detail_Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.out_storage_detail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into out_storage_detail(");
			strSql.Append("out_storage_id,material_id,material_detail_id,use_projectname,receive_address,receive_userName,receive_userPhone,receive_time,op_uid,remark,inspection_uid,nums)");
			strSql.Append(" values (");
			strSql.Append("@out_storage_id,@material_id,@material_detail_id,@use_projectname,@receive_address,@receive_userName,@receive_userPhone,@receive_time,@op_uid,@remark,@inspection_uid,@nums)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@out_storage_id", SqlDbType.Int,4),
					new SqlParameter("@material_id", SqlDbType.Int,4),
					new SqlParameter("@material_detail_id", SqlDbType.VarChar,50),
					new SqlParameter("@use_projectname", SqlDbType.NVarChar,100),
					new SqlParameter("@receive_address", SqlDbType.VarChar,300),
					new SqlParameter("@receive_userName", SqlDbType.NVarChar,30),
					new SqlParameter("@receive_userPhone", SqlDbType.VarChar,20),
					new SqlParameter("@receive_time", SqlDbType.DateTime),
					new SqlParameter("@op_uid", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,300),
					new SqlParameter("@inspection_uid", SqlDbType.Int,4),
            new SqlParameter("@nums", SqlDbType.Int,4),};
			parameters[0].Value = model.out_storage_id;
			parameters[1].Value = model.material_id;
			parameters[2].Value = model.material_detail_id;
			parameters[3].Value = model.use_projectname;
			parameters[4].Value = model.receive_address;
			parameters[5].Value = model.receive_userName;
			parameters[6].Value = model.receive_userPhone;
			parameters[7].Value = model.receive_time;
			parameters[8].Value = model.op_uid;
			parameters[9].Value = model.remark;
			parameters[10].Value = model.inspection_uid;
            parameters[11].Value = model.nums;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public bool UpdatePic(int out_storage_detail_Id, string file)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update out_storage_detail set ");
            strSql.Append("ofile=@ofile ");            
            strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
            SqlParameter[] parameters = {                
                    new SqlParameter("@ofile", SqlDbType.VarChar,50),                   
                    new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)};
            parameters[0].Value = file;
            parameters[1].Value = out_storage_detail_Id;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool UpdateEx(lcs.Model.out_storage_detail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update out_storage_detail set ");           
            strSql.Append("receive_address=@receive_address,");
            strSql.Append("receive_userName=@receive_userName,");
            strSql.Append("receive_userPhone=@receive_userPhone,");
            strSql.Append("receive_time=@receive_time,");
            strSql.Append("nums=@nums,");
            strSql.Append("jd=@jd,");
            strSql.Append("wd=@wd,");
            strSql.Append("out_name=@out_name,");
            strSql.Append("remark=@remark");
            
            strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
            SqlParameter[] parameters = {
                    
                    new SqlParameter("@receive_address", SqlDbType.VarChar,300),
                    new SqlParameter("@receive_userName", SqlDbType.NVarChar,30),
                    new SqlParameter("@receive_userPhone", SqlDbType.VarChar,20),
                    new SqlParameter("@receive_time", SqlDbType.DateTime),
                    new SqlParameter("@nums", SqlDbType.Decimal,9),
                    new SqlParameter("@jd", SqlDbType.Decimal,9),
                    new SqlParameter("@wd", SqlDbType.Decimal,9),
                    new SqlParameter("@out_name", SqlDbType.VarChar,30),
                    new SqlParameter("@remark", SqlDbType.VarChar,300),
                    
                    new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.receive_address;
            parameters[1].Value = model.receive_userName;
            parameters[2].Value = model.receive_userPhone;
            parameters[3].Value = model.receive_time;
            parameters[4].Value = model.nums;
            parameters[5].Value = model.jd;
            parameters[6].Value = model.wd;
            parameters[7].Value = model.out_name; 
            parameters[8].Value = model.remark;
            parameters[9].Value = model.out_storage_detail_Id;



            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(lcs.Model.out_storage_detail model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update out_storage_detail set ");
			strSql.Append("out_storage_id=@out_storage_id,");
			strSql.Append("material_id=@material_id,");
			strSql.Append("material_detail_id=@material_detail_id,");
			strSql.Append("use_projectname=@use_projectname,");
			strSql.Append("receive_address=@receive_address,");
			strSql.Append("receive_userName=@receive_userName,");
			strSql.Append("receive_userPhone=@receive_userPhone,");
			strSql.Append("receive_time=@receive_time,");
			strSql.Append("op_uid=@op_uid,");
			strSql.Append("remark=@remark,");
			strSql.Append("inspection_uid=@inspection_uid");
			strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@out_storage_id", SqlDbType.Int,4),
					new SqlParameter("@material_id", SqlDbType.Int,4),
					new SqlParameter("@material_detail_id", SqlDbType.VarChar,50),
					new SqlParameter("@use_projectname", SqlDbType.NVarChar,100),
					new SqlParameter("@receive_address", SqlDbType.VarChar,300),
					new SqlParameter("@receive_userName", SqlDbType.NVarChar,30),
					new SqlParameter("@receive_userPhone", SqlDbType.VarChar,20),
					new SqlParameter("@receive_time", SqlDbType.DateTime),
					new SqlParameter("@op_uid", SqlDbType.Int,4),
					new SqlParameter("@remark", SqlDbType.VarChar,300),
					new SqlParameter("@inspection_uid", SqlDbType.Int,4),
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)};
			parameters[0].Value = model.out_storage_id;
			parameters[1].Value = model.material_id;
			parameters[2].Value = model.material_detail_id;
			parameters[3].Value = model.use_projectname;
			parameters[4].Value = model.receive_address;
			parameters[5].Value = model.receive_userName;
			parameters[6].Value = model.receive_userPhone;
			parameters[7].Value = model.receive_time;
			parameters[8].Value = model.op_uid;
			parameters[9].Value = model.remark;
			parameters[10].Value = model.inspection_uid;
			parameters[11].Value = model.out_storage_detail_Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int out_storage_detail_Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from out_storage_detail ");
			strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = out_storage_detail_Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string out_storage_detail_Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from out_storage_detail ");
			strSql.Append(" where out_storage_detail_Id in ("+out_storage_detail_Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.out_storage_detail GetModel(int out_storage_detail_Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 out_storage_detail_Id,out_storage_id,material_id,material_detail_id,use_projectname,receive_address,receive_userName,receive_userPhone,receive_time,op_uid,remark,inspection_uid,nums,jd,wd,out_name,ostatus,ofile from out_storage_detail ");
			strSql.Append(" where out_storage_detail_Id=@out_storage_detail_Id");
			SqlParameter[] parameters = {
					new SqlParameter("@out_storage_detail_Id", SqlDbType.Int,4)
			};
			parameters[0].Value = out_storage_detail_Id;

			lcs.Model.out_storage_detail model=new lcs.Model.out_storage_detail();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.out_storage_detail DataRowToModel(DataRow row)
		{
			lcs.Model.out_storage_detail model=new lcs.Model.out_storage_detail();
			if (row != null)
			{
				if(row["out_storage_detail_Id"]!=null && row["out_storage_detail_Id"].ToString()!="")
				{
					model.out_storage_detail_Id=int.Parse(row["out_storage_detail_Id"].ToString());
				}
				if(row["out_storage_id"]!=null && row["out_storage_id"].ToString()!="")
				{
					model.out_storage_id=int.Parse(row["out_storage_id"].ToString());
				}
				if(row["material_id"]!=null && row["material_id"].ToString()!="")
				{
					model.material_id=int.Parse(row["material_id"].ToString());
				}
				if(row["material_detail_id"]!=null)
				{
					model.material_detail_id=row["material_detail_id"].ToString();
				}
				if(row["use_projectname"]!=null)
				{
					model.use_projectname=row["use_projectname"].ToString();
				}
				if(row["receive_address"]!=null)
				{
					model.receive_address=row["receive_address"].ToString();
				}
				if(row["receive_userName"]!=null)
				{
					model.receive_userName=row["receive_userName"].ToString();
				}
				if(row["receive_userPhone"]!=null)
				{
					model.receive_userPhone=row["receive_userPhone"].ToString();
				}
				if(row["receive_time"]!=null && row["receive_time"].ToString()!="")
				{
					model.receive_time=DateTime.Parse(row["receive_time"].ToString());
				}
				if(row["op_uid"]!=null && row["op_uid"].ToString()!="")
				{
					model.op_uid=int.Parse(row["op_uid"].ToString());
				}
				if(row["remark"]!=null)
				{
					model.remark=row["remark"].ToString();
				}
				if(row["inspection_uid"]!=null && row["inspection_uid"].ToString()!="")
				{
					model.inspection_uid=int.Parse(row["inspection_uid"].ToString());
				}
                if (row["nums"] != null && row["nums"].ToString() != "")
                {
                    model.nums = decimal.Parse(row["nums"].ToString());
                }
                if (row["jd"] != null && row["jd"].ToString() != "")
                {
                    model.jd = decimal.Parse(row["jd"].ToString());
                }
                if (row["wd"] != null && row["wd"].ToString() != "")
                {
                    model.wd = decimal.Parse(row["wd"].ToString());
                }
                if (row["ostatus"] != null && row["ostatus"].ToString() != "")
                {
                    model.ostatus = int.Parse(row["ostatus"].ToString());
                }
                if (row["out_name"] != null)
                {
                    model.out_name = row["out_name"].ToString();
                }
                if (row["ofile"] != null)
                {
                    model.ofile = row["ofile"].ToString();
                }
            }
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select out_storage_detail_Id,out_storage_id,material_id,material_detail_id,use_projectname,receive_address,receive_userName,receive_userPhone,receive_time,op_uid,remark,inspection_uid,user_name,material,equipmentManage,nums,jd,wd,[out_name],[ostatus],[ofile] ");
            strSql.Append(" FROM Vout_storage_detail where 1=1");
			if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet GetListTZ(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VmaterialTZ where 1=1");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetListMoney(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [out_storage_detail_Id],[out_storage_id],[material_id],[material_detail_id],[use_projectname],[receive_address],[receive_userName],[receive_userPhone],[receive_time],[op_uid],[remark],[inspection_uid],[user_name],[money],[useyear] ,material FROM [Voutmoney] where 1=1");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" out_storage_detail_Id,out_storage_id,material_id,material_detail_id,use_projectname,receive_address,receive_userName,receive_userPhone,receive_time,op_uid,remark,inspection_uid ");
			strSql.Append(" FROM out_storage_detail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM out_storage_detail ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.out_storage_detail_Id desc");
			}
			strSql.Append(")AS Row, T.*  from out_storage_detail T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "out_storage_detail";
			parameters[1].Value = "out_storage_detail_Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
        public bool Updateuid(int uid, string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update out_storage_detail set ");
            strSql.Append("inspection_uid=@inspection_uid ");
            strSql.Append(" where out_storage_detail_Id in(" + ids + ")");
            SqlParameter[] parameters = {
                    new SqlParameter("@inspection_uid", SqlDbType.Int,4)
                    };
            parameters[0].Value = uid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更改分发巡检任务的人员id，out_storage_detail表中的op_uid
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Updateopuid(int uid, string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update out_storage_detail set ");
            strSql.Append("op_uid=@op_uid ");
            strSql.Append(" where out_storage_detail_Id in(" + ids + ")");
            SqlParameter[] parameters = {
                    new SqlParameter("@op_uid", SqlDbType.Int,4)
                    };
            parameters[0].Value = uid;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetListByVout_storage_detail_materialinfo(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Vout_storage_detail_materialinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  ExtensionMethod
    }
}

